package com.es.hr.baoxiao;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.es.common.CommonBean;
import com.es.common.CommonBiz;
import com.es.main.LoginUserBeen;

import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class CreateBaoxiaoAction  extends ActionSupport
implements ServletRequestAware{
	
	HttpServletRequest request;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}
	
	public String execute() throws Exception{
		String result = "";
		String message = "";
		LoginUserBeen userLogin = (LoginUserBeen)request.getSession().getAttribute("loginuser");
		String role = userLogin.getLoginRole();
		BaoxiaoBiz baoxiaoBiz = new BaoxiaoBiz();
		CommonBean shiyebu = new CommonBiz().getCommonByTypeAndCode("shiyebu", "ITMall");
		
		//只有事业部itmall的ctm才能申请报销
		if(!role.equals("CTM") && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){
			message = "只有ITMall事业部的CTM才能创建报销";
			this.request.setAttribute("message", message);
			return "error";
		}
		
		Date date = new Date();
		int year = date.getYear() + 1900;
		int mouth = date.getMonth() + 1;
		int day = date.getDate();
		
		String syear = new Integer(year).toString();
		String smouth = new Integer(mouth).toString();
		if(smouth.length() < 2) smouth = "0" + smouth;
		String sday = new Integer(day).toString();
		if(sday.length() < 2) sday = "0" + sday;
		
		//限制5号以内才能报销
		com.es.common.CommonBean riqibean = new CommonBiz().getCommonByTypeAndCode("baoxiaoproperty", "baoxiaoshenqingriqi");
		int riqi = riqibean == null?3:new Integer(riqibean.getCommonValue()).intValue();
		if(day > riqi){
			message = "每个月前"+riqi+"天才能报销";
			this.request.setAttribute("message", message);
			return "error";
		}
		BaoxiaoBean bean = baoxiaoBiz.getBaoxiaoBaseInfoFromEmp(userLogin.getLoginId());
		bean.baoxiao_time = syear + "-" +  smouth + "-" + sday;
		
		String startTime = syear + "-" +  smouth + "-" + "00";
		String endTime = syear + "-" +  smouth + "-" + "32";
		boolean hasBaoxiao = baoxiaoBiz.hasBaoxiaothisMouth(bean.ctm_code, startTime, endTime);
		if(hasBaoxiao){
			message = "每个月只能报销一次！";
			this.request.setAttribute("message", message);
			return "error";
		}
		this.request.setAttribute("baoxiaoBean", bean);
		this.request.setAttribute("classList", new String[]{"交通", "住宿","餐补"});
		return "success";
	}
	
}
